<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Building;
use App\Admin\Repositories\NewHouse;
use App\library\services\FangAttrsService;
use App\Models\Cities;
use App\Models\FangAttrs;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class NewHouseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new NewHouse(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('building_id');
            $grid->column('house_type_id');
            $grid->column('forward');
            $grid->column('fit_up');
            $grid->column('area');
            $grid->column('avg_price');
            $grid->column('price');
            $grid->column('floor');
            $grid->column('category');
            $grid->column('images');
            $grid->column('desc');
            $grid->column('latitude');
            $grid->column('longitude');
            $grid->column('sale_status');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new NewHouse(), function (Show $show) {
            $show->field('id');
            $show->field('building_id');
            $show->field('house_type_id');
            $show->field('forward');
            $show->field('fit_up');
            $show->field('area');
            $show->field('avg_price');
            $show->field('price');
            $show->field('floor');
            $show->field('category');
            $show->field('images');
            $show->field('desc');
            $show->field('latitude');
            $show->field('longitude');
            $show->field('sale_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $data = \App\Models\Building::all()->toArray();
        $buildingIds = [];
        foreach ($data as $value) {
            $buildingIds[$value['id']] = $value['title'];
        }
        $directions = FangAttrsService::getFangDirection();
        $arr = [];
        foreach ($directions as $direction) {
            $arr[$direction['id']] = $direction['name'];
        }
        return Form::make(new NewHouse(), function (Form $form) use ($buildingIds,$arr) {
            $form->display('id');
            $form->select('building_id')->options($buildingIds)->load('house_type_id', '/type');
            $form->select('house_type_id');
            $form->select('forward')->options($arr);
            $form->text('fit_up');
            $form->text('area');
            $form->text('address','详细地址');
            $form->text('avg_price');
            $form->text('price');
            $form->text('floor');
            $form->text('category');
            $form->multipleImage('images');
            $form->editor('desc');
            $form->switch('sale_status');
            // 省份
            $form->select('province')->options(
                Cities::where('pid', 0)->get()->pluck('name', 'id')
            )->load('city', '/city');
            // 城市
            $form->select('city')->load('region', '/city');
            // 区域
            $form->select('region');
            $form->display('created_at');
            $form->display('updated_at')->value(time());

        });
    }
}
